<?php
require_once './common.php';
$title="获取SID";
include ('./head.php');
echo '<div class="header">
        <ul class="nav nav-pills pull-right" role="tablist">
          <li role="presentation" class="active"><a href="../index.php">首页</a></li>
          <li role="presentation"><a href="../index.php?action=my">用户中心</a></li>
          <li role="presentation"><a href="../login.php?action=logout">退出</a></li>
        </ul>
        <h3 class="text-muted" align="left">获取SID</h3>
      </div><hr>';
if(!isset($_POST['qq'])){
$p=base64_decode($_GET['p']);
echo <<<HTML
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src="login.js"></script>
<script type="text/javascript">
var xiha={
	postData: function(url, parameter, callback, dataType, ajaxType) {
		if(!dataType) dataType='json';
		$.ajax({
			type: "POST",
			url: url,
			async: true,
			dataType: dataType,
			json: "callback",
			data: parameter,
			success: function(data) {
				if (callback == null) {
					return;
				} 
				callback(data);
			},
			error: function(error) {
				alert('创建连接失败');
			}
		});
	}
}
function login(uin,p,vcode,pt_verifysession){
	$('#load').html('正在登陆，请稍等...');
	var loginurl="login.php?do=qqlogin";
	xiha.postData(loginurl,"uin="+uin+"&p="+p+"&vcode="+vcode+"&pt_verifysession="+pt_verifysession+"&r="+Math.random(1), function(d) {
		if(d.saveOK ==0){
			$('#load').html('SID获取成功，请稍等...');
			window.location.href="add.php?qq="+uin+"&sid="+d.sid+"&skey="+d.skey;
		}else if(d.saveOK ==4){
			$('#load').html('验证码错误，请重新登录。');
			$('#submit').attr('do','submit');
			$('.code').hide();
		}else if(d.saveOK ==3){
			$('#load').html('您输入的帐号或密码不正确，请重新输入密码！');
			$('#submit').attr('do','submit');
			$('.code').hide();
		}else{
			alert(d.msg);
			$('#submit').attr('do','submit');
		}
	});
	
}

function getvc(uin,sig){
	$('#load').html('获取验证码，请稍等...');
	var getvcurl="login.php?do=getvc";
	xiha.postData(getvcurl,'uin='+uin+'&sig='+sig+'&r='+Math.random(1), function(d) {
		if(d.saveOK ==0){
			$('#load').html('请输入验证码');
			$('#codeimg').attr('vc',d.vc);
			$('#codeimg').html('<img onclick="getvc(\''+uin+'\',\''+d.vc+'\')" src="login.php?do=getvcpic&uin='+uin+'&sig='+d.vc+'&r='+Math.random(1)+'">');
			$('#submit').attr('do','code');
			$('.code').show();
		}else{
			alert(d.msg);
		}
	});

}
function dovc(uin,code,vc){
	$('#load').html('验证验证码，请稍等...');
	var getvcurl="login.php?do=dovc";
	xiha.postData(getvcurl,'uin='+uin+'&ans='+code+'&sig='+vc+'&r='+Math.random(1), function(d) {
		if(d.rcode ==0){
			var pwd=$('#pwd').val();
			p=getmd5(uin,pwd,d.randstr.toUpperCase());
			login(uin,p,d.randstr.toUpperCase(),d.sig);
			
		}else{
			$('#load').html('验证码错误，重新生成验证码，请稍等...');
			getvc(uin,vc);
		}
	});

}
$(document).ready(function(){
	$('#submit').click(function(){
		var self=$(this);
		$('#load').html('登录中，请稍候...');
		var uin=$('#uin').val(),
			pwd=$('#pwd').val();
		if(self.attr('do') == 'code'){
			var vcode=$('#code').val(),
				vc=$('#codeimg').attr('vc');
			dovc(uin,vcode,vc);
		}else{
		if (self.attr("data-lock") === "true") return;
			else self.attr("data-lock", "true");
		var checkvcurl="login.php?do=checkvc";
		xiha.postData(checkvcurl,'uin='+uin+'&r='+Math.random(1), function(d) {
			if(d.saveOK ==0){
				var strs= new Array(); //定义一数组
				strs=d.data.split(",");
				if(strs[0]==0){
					pt_verifysession=strs[3];
					p=getmd5(uin,pwd,strs[2]);
					login(strs[1],p,strs[2],pt_verifysession);
				}else{
					getvc(uin,strs[2]);
				}
			}else{
				alert(d.msg);
				$('#load').html('');
			}
			self.attr("data-lock", "false");
		});
		}
	});
});
</script>
<h3 class="form-signin-heading">添加QQ</h3>
QQ帐号:<br><input type="text" class="form-control" id="uin" value="{$_GET['qq']}"><br>
QQ密码:<br><input type="password" class="form-control" id="pwd" value="{$p}"><br>
<div class="formrow code" style="display: none;">
			<div style="width:80px;float: left;">输入验证码：</div>
			<span id="codeimg"></span><input type="text" id="code" style="height: 50px;font-size: 30px;width: 100px;margin-left: 15px;">
		</div>
		<div class="panel panel-primary" id="load" style="color: red;border: 1px solid #124F0B;text-align: center;"></div>
<input type="button" class="btn btn-primary btn-block" id="submit" value="确认添加"><br>
<a href="add.php" class="btn btn-default btn-block">已知SID/SKEY免密码挂机</a>
</form>
HTML;
}
$w_sid=substr(md5("wnyr".time()),0,24);
if(!isset($_GET['verify'])&&isset($_POST['qq'])){
	$qq=$_POST['qq'];
	$qpwd=$_POST['qpwd'];
	
	$url="http://pt.3g.qq.com/handleLogin?";
	$data="&sid=" . $w_sid . "&qq=" . $qq . "&pwd=" . $qpwd . "&bid_code=3GQQ&toQQchat=true&login_url=http://pt.3g.qq.com/s?aid=nLoginnew&&q_from=&modifySKey=2&loginType=1&aid=nLoginHandle";
	$str=cpost($url,$data);
	//print_r($str);

}else{
	$qq=$_POST["qq"];
	$qpwd=$_POST['qpwd'];
	$u_token=$_POST["u_token"];
	$hexpwd=$_POST["hexpwd"];
	$sid=$_POST["sid"];
	$hexp=$_POST["hexp"];
	$nopre=$_POST["nopre"];
	$auto=$_POST["auto"];
	$loginTitle=$_POST["loginTitle"];
	$q_from=$_POST["q_from"];
	$modifySKey=$_POST["modifySKey"];
	$q_status=$_POST["q_status"];
	$r=$_POST["r"];
	$loginType=$_POST["loginType"];
	$login_url=$_POST["login_url"];
	$extend=$_POST["extend"];
	$r_sid=$_POST["r_sid"];
	$bid_code=$_POST["bid_code"];
	$bid=$_POST["bid"];
	$toQQchat=$_POST["toQQchat"];
	$rip=$_POST["rip"];
	
	$verify=$_POST["verify"];
	$url="http://pt.3g.qq.com/handleLogin?sid=" . $w_sid . "&vdata=";
	$data="&qq={$qq}&u_token={$u_token}&hexpwd={$hexpwd}&sid={$sid}&hexp={$hexp}&nopre={$nopre}&auto={$auto}&loginTitle={$loginTitle}&q_from={$q_from}&modifySKey={$modifySKey}&q_status={$q_status}&r={$r}&loginType={$loginType}&login_url={$login_url}&extend={$extend}&r_sid={$r_sid}&bid_code={$bid_code}&bid={$bid}&toQQchat={$toQQchat}&rip={$rip}&verify={$verify}";
	$str=cpost($url,$data);
	
}

if(strstr($str,'验证码')){
	#验证码处理
	$value=explode('</form>',html_entity_decode($str));
	preg_match('/<img.*src=("|\')(.*)\\1.*alt=\\1验证码\\1.*\/.*>/iU',$value[0],$img);
	$value=explode('请输入上图字符',$value[1]);
	$value=preg_replace("/<(\/?form.*?)>/si","",$value[0]);
		//print_r($value);

	echo <<<HTML
<div class="t"><div>验证码处理： </div>
QQ帐号：{$qq}<br/>系统检测到您的操作异常，为保证您的号码安全，请输入验证码进行验证，防止他人盗取密码。<br/><img src="{$img[2]}" alt="请开启图片显示"/>
<form action="getsid.php?verify=" method="post">
请输入验证码(不区分大小写)： <br/>
<input type="text" name="verify" class="form-control" value=""/>
<input type="hidden" name="qpwd" value="{$qpwd}"/>
{$value}
<br/><input type="submit" class="btn btn-primary btn-block" value="确定/换一张" class="ok"/>
</form>
</div>
	
HTML;
}else{
	
	if(strstr($str,'The URL has moved')){

		$sid=getsid($str);
		$p=base64_encode($_POST['qpwd']);
		echo '<h3>获取成功</h3><font style="color:green;">QQ:'.$_POST['qq'].'<br/>SID:'.$sid[2].'</font><br/><meta http-equiv="refresh" content="3;url=add.php?qq='.$_POST['qq'].'&p='.$p.'&sid='.$sid[2].'">获取成功，3秒后自动跳转...<br>若无法跳转请<a href="add.php?qq='.$_POST['qq'].'&p='.$p.'&sid='.$sid[2].'">点此刷新</a>';
	}elseif(strstr($str,'帐号或密码不正确')){
		echo '您输入的帐号或密码不正确<br />3秒后跳转到登陆页...<br />若无法跳转请<a href="getsid.php?action=getsid">点击这里进入</a><meta http-equiv="refresh" content="3;url=getsid.php?action=getsid">';
	}elseif(isset($_POST['qq'])){
		echo '未知错误，请检查帐号或者密码是否为空，此次登陆失败，<br />3秒后跳转到登陆页...<br />若无法跳转请<a href="getsid.php?action=getsid">点击这里进入</a><meta http-equiv="refresh" content="3;url=getsid.php?action=getsid">';
	}
		//if(isset($_POST['qq'])) echo '抓取数据失败<br />3秒后跳转到登陆页...<br />若无法跳转请<a href="getsid.php?action=getsid">点击这里进入</a><meta http-equiv="refresh" content="3;url=getsid.php?action=getsid">';

}
include ('./foot.php');
exit;
?>